package net.sunniwell.location;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

import android.text.TextUtils;
import android.util.Log;

public class LocationUtil {

	public static final String sGetAddrUrl = "http://ip-api.com/json/";

	public static LocateCityBean locateCityBean() {
		LocateCityBean locateCityBean = null;
		try {
			HttpClient httpClient = new DefaultHttpClient();
			String requestStr = sGetAddrUrl;
			HttpGet request = new HttpGet(requestStr);
			HttpResponse response = httpClient.execute(request);
			if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				String cityJson = EntityUtils.toString(response.getEntity());
				Log.d("zpc", "locateCityName" + cityJson);
				if (!TextUtils.isEmpty(cityJson)) {
					Gson gson = new Gson();
					locateCityBean = gson.fromJson(cityJson, LocateCityBean.class);
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return locateCityBean;
	}
}